<?php
class Admin_UserController extends Zend_Controller_Action
{
	private $_model;
	private $auth;
	private $acl;
	public function init()
	{
		
		$this->_model = new Admin_Model_User();
		$this->auth = Zend_Auth::getInstance();
		$this->acl = new Admin_Model_Access();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/admin";
		$dirlayout = APPLICATION_PATH . "/layouts/admin";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/style.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-ui-1.8.18.custom.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/nicEdit.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slidedown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/checkbox.js');
		$this->view->headScript()->appendFile($dirfile . '/js/deleteUser.js');
		$this->view->headScript()->appendFile($dirfile . '/js/editor.js');
		$this->view->headScript()->appendFile($dirfile . '/js/adduser.js');
		$this->view->headScript()->appendFile($dirfile . '/js/edituser.js');
		$this->view->headScript()->appendFile($dirfile . '/js/newuser.js');
		$this->view->headScript()->appendFile($dirfile . '/js/active.js');
		$this->view->headScript()->appendFile($dirfile . '/js/disable.js');
		//$this->view->headScript()->appendFile($dirfile . '/js/datepick3.js');
		$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		Zend_Layout::startMvc($option);
	}
	public function indexAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','index'))
		{
		$group = $this->_model->getallgroup();
		$this->view->groups = $group;	
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function addAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','add'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$fullname = $this->_request->getParam('fullname');
		$username = $this->_request->getParam('username');
		$pass = $this->_request->getParam('pass');
		$confirmpass = $this->_request->getParam('confirmpass');
		$group = $this->_request->getParam('group');
		$address = $this->_request->getParam('address');
		$phone = $this->_request->getParam('phone');
		$email = $this->_request->getParam('email');
		$active = $this->_request->getParam('active');
		$id_group = $this->_model->getidgroup($group);
		$name_folder = 'folder'.$username;
		try {
			if ($pass == $confirmpass)
			{
			$insert = $this->_model->add($fullname, $username, md5($pass), $id_group['id_group'], $address, $phone, $email, $active,$name_folder);
			echo "Add thanh cong";
			}
			else 
			{
				echo "Pass nhap sai";
			}
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		} 
		//echo $id_group['id_group'];
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function manageAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','manage'))
		{
		$data = $this->_model->getuserAll();
		$paging = Zend_Paginator::factory($data);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->paging = $paging;
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function getsearchAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','getsearch'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$contents = $this->_request->getParam('text-search');
		$session = new Zend_Session_Namespace('searchuser');
		$session->content = $contents;
		$this->_forward('search');
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function searchAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','search'))
		{
		$session = new Zend_Session_Namespace('searchuser');
		$contents = $session->content;
		$search = $this->_model->search($contents);
		$pagingsearch = Zend_Paginator::factory($search);
		$pagingsearch->setItemCountPerPage(2);
		$pagingsearch->setPageRange(3);
		$currentsearch = $this->_request->getParam('page',1);
		$pagingsearch->setCurrentPageNumber($currentsearch);
		$this->view->pagingsearch = $pagingsearch;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function deleteAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','delete'))
		{
		$id = $this->_request->getParam('id');
		$delete = $this->_model->delete($id);
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function vieweditAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','viewedit'))
		{
		$id = $this->_request->getParam('id');
		$data = $this->_model->getId($id);
		$session = new Zend_Session_Namespace('iduser');
		$session->iduser = $id;
		$this->view->data = $data;
		}
		else {
			$this->_redirect('/error/denied');
		}
	}
	public function editAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','edit'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$session = new Zend_Session_Namespace('iduser');
		$id = $session->iduser;
		$fullname = $this->_request->getParam('fullname');
		$username = $this->_request->getParam('username');
		$pass = $this->_request->getParam('pass');
		$confirmpass = $this->_request->getParam('confirmpass');
		$group = $this->_request->getParam('group');
		$address = $this->_request->getParam('address');
		$phone = $this->_request->getParam('phone');
		$email = $this->_request->getParam('email');
		$active = $this->_request->getParam('active');
		$id_group = $this->_model->getidgroup($group);
		try
		{
			$update = $this->_model->edit($id, $fullname, $username, $pass, $id_group, $address, $phone, $email, $active);
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function activeAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','active'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		try {
			$update = $this->_model->active($id, 1);
			echo "Active Success";
		}catch (Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function disableAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'user','disable'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		try
		{
			$update = $this->_model->disable($id, 0);
			echo "Disable Success";
		}catch (Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
}